#include "File.h"
#include <iostream>
using namespace std;
File::File() {}
File::~File() {}
unsigned int File::getForestNum() { return forest.size(); }
unsigned int File::getTreesNum(string &filename) { return forest[filename]; }
void File::insertTree(string &filename)
{
    if( forest.find(filename) == forest.end() )
        forest[filename] = 1;
    else
        forest[filename]++;
}
void File::insertYear(string year)
{
    if(year == "") return;
    years.push_back(stoi(year));
}
void File::getRate()
{
    int total = 0;
    for(auto i = forest.begin();i != forest.end(); i++)
    {
        total += i->second;
    }
    for(auto i = forest.begin();i != forest.end(); i++)
    {
        double rate = i->second / (total* 1.0);
        printf("The Tree %s ,rate is %.2f%%\n",i->first.c_str(),rate * 100.0);
    }
}

void File::getYear()
{
    int total = 0;
    int f[10] = {0};
    total = years.size();
    for(auto i = years.begin();i != years.end(); i++)
    {
        if(0 <= *i && *i <10) f[0]++;
        else if(10 <= *i && *i < 20) f[1]++;
        else if(20 <= *i && *i < 30) f[2]++;
        else if(30 <= *i && *i < 40) f[3]++;
        else if(40 <= *i && *i < 50) f[4]++;
        else if(50 <= *i && *i < 60) f[5]++;
        else if(60 <= *i && *i < 70) f[6]++;
        else if(70 <= *i && *i < 80) f[7]++;
        else if(80 <= *i && *i < 90) f[8]++;
        else if(80 <= *i && *i < 100) f[9]++;
    }
    for(int i = 0; i < 9; i++)
    {
        printf("the years in %d to %d,rate is %.2lf%%\n",10*i,10*(i+1),f[i] / (total*1.0)*100);
    }
}

//
// Created by yunlang on 2021/12/15.
//

